package cn.edu.scau.cmi.zhouxinyue.composite;

import java.util.Scanner;


public class View {
	private static Composite find=null; 
	public void sout(Composite root){
		if(root.getParent()!=null) {
			System.out.print(root.getName()+"(该队伍属于:"+root.getParent().getName()+")  ");
		}
		else {
			System.out.print(root.getName());
		}
	    if(root.getTnumber()!=0) {
			int i=0;
			while(i<root.getTnumber()) {
				System.out.println();
				sout((Composite)root.getTeam().get(i));
				i++;
			}
		}
		if(root.getPnumber()!=0) {
			int i=0;
			while(i<root.getPnumber()) {	
				System.out.print(root.getPerson().get(i).getName()+"(该个人属于:"+root.getName()+")  ");
				i++;
			}
		}
	    return ;
	}
	
	public Composite search(Composite root,String name) {
		if(root.getName().equals(name)) {
			find=(Composite)root;
		}
		else if(root.getTnumber()!=0) {
			int i=0;
			while(i<root.getTnumber()) {
				search((Composite)root.getTeam().get(i),name);
				i++;
			}
		}
		return find;
	}
	
	public Composite searchperson(Composite root,String name) {
		if(root.getPnumber()!=0) {
			int i=0;
			while(i<root.getPnumber()) {
				if(root.getPerson().get(i).getName().equals(name)) {
					find=root;
				}
				i++;
			}
		}
		else {
			int i=0;
			while(i<root.getTnumber()) {
				searchperson((Composite)root.getTeam().get(i),name);
				i++;
			}
		}
		return find;	
	}
	
	public void show() {
		int choose=0;
		String name,fname;
		Composite rootR=new Composite("root",null);
		Composite root;
		View v=new View();
		Scanner reader=new Scanner(System.in);
		Composite father=null;
		Leaf newperson=null;
		Composite newteam=null;;
		while(true) {
			root=rootR;
			v.sout(rootR);
			System.out.println("1.输入个人     2.输入团队     3.删除个人      4.删除团队     5.退出");
			choose=reader.nextInt();
			switch(choose) {
			case 1:
				System.out.println("请输入要插入的个人名字:");
				name=reader.next();
				System.out.println("请输入要插入的父节点名字:");
				fname=reader.next();
				father=v.search(root,fname);
				newperson=new Leaf(name,father);
				father.addPerson(newperson);
				break;
			case 2:
				System.out.println("请输入要插入的团队名字:");
				name=reader.next();
				System.out.println("请输入要插入的父节点名字:");
				fname=reader.next();
				father=v.search(root,fname);
				newteam=new Composite(name,father);
				father.addTeam(newteam);
				break;
			case 3:
				System.out.println("请输入要删除的个人名字:");
				name=reader.next();
				father=v.searchperson(root, name);
				father.deletePerson(new Leaf(name,null));
				break;
			case 4:
				System.out.println("请输入要删除的团队名字:");
				name=reader.next();
				father=v.search(root, name);
				father.getParent().deleteTeam(new Composite(name,null));
				break;
			case 5:
				System.exit(0);
				break;
			}
		}
	}

}
